
import { Component, OnInit, DoCheck, Input, OnChanges, SimpleChanges, ViewChild, ElementRef } from '@angular/core';
declare var Snap: any, mina: any;
import { Router, ActivatedRoute, Params } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Validators } from '@angular/forms';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
// import { forbiddenNameValidator } from '../shared/forbidden-name.directive';
import LCalendar from "../../assets/js/LCalendar.js";
import { ConmonService } from "../../service/common.service";
// declare var $: any;
import * as $ from 'jquery';
export class WorkModify {
  SubmitDate: string;
  // 报工日期
  WorkDate: string;
  WorkType: string;
  // 项目名称
  ProName: string;
  //  项目编号
  ProId: string;
  // 报工编号
  SubmitNo: string;
  // 工作内容
  WorkContent: string;
  // 消耗工时
  ConsumeTime: any;
}
@Component({
  selector: 'app-work-modify',
  templateUrl: './work-modify.component.html',
  styleUrls: ['./work-modify.component.scss']
})
export class WorkModifyComponent implements OnInit {
  title: string = "报工添加";
  SubmitNo: any;
  tokenObj: any;
  dataList: any;
  @Input() WorkModify: WorkModify = {
    SubmitNo: '',
    SubmitDate: '',
    WorkDate: '',
    ProName: '',
    WorkType: '',
    ProId: '',
    WorkContent: '',
    ConsumeTime: ''
  };
  private iswork: boolean = false;
  private isAddWork: boolean = false;
  private isleave: boolean = false;
  private isFishesd: boolean = false;
  private projectNameFlag: boolean = false;
  private projectIdFlag: boolean = false;
  private dataObj:any;
  changeColor: boolean = true;
  @ViewChild('submitBtn')
  submitBtn: ElementRef;
  ngDoCheck() {
    const ifdisabled = this.submitBtn.nativeElement.disabled;
    console.log(this.WorkModify);
    this.changeColor = true;
    if (!ifdisabled) {
      this.changeColor = true;
    } else {
      this.changeColor = false;
    }
  };
  // 确定按钮是否可用
  submitted = false;
  //  onSubmit() { this.submitted = true; }
  choiceWorkType($event) {
    var val = $event.target.value;
    if (val === '工作') {
      this.iswork = true;
      this.isleave = false;
      this.isAddWork = false;
      this.WorkModify.WorkType = '0';
    } else if (val === '加班') {
      this.isAddWork = true;
      this.isleave = false;
      this.iswork = false;
      this.WorkModify.WorkType = '1';
    } else if (val === '请假') {
      this.isleave = true;
      this.isAddWork = false;
      this.iswork = false;
      this.WorkModify.WorkType = '2';
    }
  };
  ngAfterViewInit() {
    // if (document.getElementById('employeeDate')) {
    //   // 我的报工添加的选择报工日期日历插件调用
    //   var calendar = new LCalendar();
    //   calendar.init({
    //     'trigger': '#employeeDate', //标签id
    //     'type': 'date', //date 调出日期选择 datetime 调出日期时间选择 time 调出时间选择 ym 调出年月选择,
    //     'minDate': '1900-1-1', //最小日期
    //     'maxDate': new Date().getFullYear() + 10 + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() //最大日期
    //   });
    // }
  };
  keyChangeColor(value: string,obj) {
    this.projectNameFlag = true;
    console.log(obj.target.nextElementSibling);
    this.dataObj = {
      SearchProType: '0',
      ProName: value,
      ProId: '',
      ProMana: ''
    };
   this.dataList = this.ConmonService.withBodyData(this.dataObj,'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function(){
      let dsa = $(obj.target.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value,"<span class='col_Color' style='color:#54a3ff;'>"+value+"</span>");
        $(obj.target.nextElementSibling).find('li').eq(t).html(x);
      };
    },0)
  };
  //为项目名称搜索出来的结果赋值
  addProjectName (name) {
    console.log(name);
    this.WorkModify.ProName = name;
    this.projectNameFlag = false;
    // this.dataList = '';
  };
  //项目名称失去焦点
  pronameBlur () {
    let _this = this;
    setTimeout(function(){
      _this.projectNameFlag = false;
    },301)
    // this.dataList = '';
  };
  //项目编号搜索
  projectIdSearch (value: string,obj) {
    this.projectIdFlag = true;
    console.log(value);
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: value,
      ProMana: ''
    };
   this.dataList = this.ConmonService.withBodyData(this.dataObj,'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function(){
      let dsa = $(obj.target.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value,"<span class='col_Color' style='color:#54a3ff;'>"+value+"</span>");
        $(obj.target.nextElementSibling).find('li').eq(t).html(x);
      };
    },0)
  };
  //项目编号数去脚垫 
  projectNumberBlur () {
    // this.dataList = '';
    let _this = this;
    setTimeout(function(){
      _this.projectIdFlag = false;
    },301)
  };
  //为项目编号搜索出来的结果添加赋值
  addProjectId (num) {
    this.WorkModify.ProId = num;
    // this.dataList = '';
    this.projectIdFlag = false;
  };
  // 发送请求，从后台传输数据     
  ngSubmit($event) {
    console.log(this.WorkModify);
    this.ConmonService.withBodyData(this.WorkModify, 'UpdateDailyWork');
    this.addSucess();
  }
  // 报工添加点击确定判断是否跳转
  addSucess(): void {
    this.router.navigate(['/PersonTatal', false]);
  }
  // 获取当前年月日
  getcurrentYYR() {
    let myDate = new Date();
    this.WorkModify.SubmitDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
  };
  // 以下是表单验证部分  
  ngOnInit() {
    this.getcurrentYYR();

    // 初始化获取数据信息
    let currentType = this.WorkModify.WorkType;
    if (currentType === '0') {
      this.iswork = true;
    } else if (currentType === '1') {
      this.isAddWork = true;
    } else if (currentType === '2') {
      this.isleave = true;
    };

  }
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private http: HttpClient,
    private ConmonService: ConmonService,
    private routerIonfo: ActivatedRoute

  ) {
    this.WorkModify.SubmitNo = this.routerIonfo.snapshot.queryParams["id"];
    var dataObj = {
      SubmitNo: this.WorkModify.SubmitNo
    };
    // this.tokenObj = ConmonService.getToken(1, dataObj, 'DailyWorkDetail', false);
    this.WorkModify = ConmonService.withBodyData(dataObj, "DailyWorkDetail").body;
    console.log(this.WorkModify)
  }

}
